package system;

/**
 * Class Validator
 * 
 * @author Rezan Achmad
 */
public class Validator {

    private static Validator _INSTANCE;

    public static Validator getInstance() {
        if (_INSTANCE == null) {
            _INSTANCE = new Validator();
        }

        return _INSTANCE;
    }
    
    public boolean equal(String s, Model model, String varName, String comparator, String comparatorVarName, String errorMessage) {
        if (!s.equals(comparator)) {
            model.addError(varName, errorMessage);
            return false;
        }

        return true;
    }
    
    public boolean equal(String s, Model model, String varName, String comparator, String comparatorVarName) {
        return equal(s, model, varName, comparator, comparatorVarName, "Nilai " + model.getAttributesLabel(varName) + " harus sama dengan " + model.getAttributesLabel(comparatorVarName));
    }

    public boolean notEmptyString(String s, Model model, String varName, String errorMessage) {
        if (isEmptyString(s)) {
            model.addError(varName, errorMessage);
            return false;
        }

        return true;
    }

    public boolean notEmptyString(String s, Model model, String varName) {
        return notEmptyString(s, model, varName, model.getAttributesLabel(varName) + " tidak boleh kosong.");
    }

    public boolean isEmptyString(String s) {
        return s == null || "".equals(s);
    }
}
